package org.gthelper.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class FileInfo {

	private String userID;
	private String filename;
	private int pixelsize_x;
	private int pixelsize_y;
	private double env_max_x;
	private double env_min_x;
	private double env_max_y;
	private double env_min_y;
	private String cRS_decode;
	private int split_size;
	private int split_num_x;
	private int split_num_y;
	private int residule;
	private int status;
	
	
	private FileInfo()
	{
		
	}
	private FileInfo(String userID,String filename,int pixelsize_x,int pixelsize_y,double env_max_x,double env_min_x,
			double env_max_y,double env_min_y,String CRS,int split_size,int split_num_x,int split_num_y,int residule,int status)
			{
		      this.userID=userID;
		      this.filename=filename;
		      this.pixelsize_x=pixelsize_x;
		      this.pixelsize_y=pixelsize_y;
		      this.env_max_x=env_max_x;
		      this.env_max_y=env_max_y;
		      this.env_min_x=env_min_x;
		      this.env_min_y=env_min_y;
		      this.cRS_decode=CRS;
		      this.split_size=split_size;
		      this.split_num_x=split_num_x;
		      this.split_num_y=split_num_y;
		      this.residule=residule;
		      this.status=status;
			}
	public String getuserID()
	{
		return this.userID;
	}
	public String getfilename()
	{
		return this.filename;
	}
	public int getpixelsize_x()
	{
		return this.pixelsize_x;
	}
	public int getpixelsize_y()
	{
		return this.pixelsize_y;
	}
	public double getenv_max_x()
	{
		return this.env_max_x;
	}
	public double getenv_min_x()
	{
		return this.env_min_x;
	}
	public double getenv_max_y()
	{
		return this.env_max_y;
	}
	public double getenv_min_y()
	{
		return this.env_min_y;
	}
	public int getsplit_size()
	{
		return this.split_size;
	}
	public int getsplit_num_x()
	{
		return this.split_num_x;
	}
	public int getsplit_num_y()
	{
		return this.split_num_y;
	}
	public int getresidule()
	{
		return this.residule;
	}
	public int getstatus()
	{
		return this.status;
	}
	public String getCRS_decode()
	{
		return this.cRS_decode;
	}
	
	public void setpixelsize_x(int pixelsize_x)
	{
		this.pixelsize_x=pixelsize_x;
	}
	public void setpixelsize_y(int pixelsize_y)
	{
		this.pixelsize_y=pixelsize_y;
	}
	public void setenv_max_x(double env_max_x)
	{
		this.env_max_x=env_max_x;
	}
	public void setenv_max_y(double env_max_y)
	{
		this.env_max_y=env_max_y;
	}
	public void setenv_min_x(double env_min_x)
	{
		this.env_min_x=env_min_x;
	}
	public void setenv_min_y(double env_min_y)
	{
		this.env_min_y=env_min_y;
	}
	public void setCRS_decode(String CRS)
	{
		this.cRS_decode=CRS;
	}
	public void setsplit_size(int split_size)
	{
		this.split_size=split_size;
	}
	public void setsplit_num_x(int split_num_x)
	{
		this.split_num_x=split_num_x;
	}
	public void setsplit_num_y(int split_num_y)
	{
		this.split_num_y=split_num_y;
	}
	public void setresidule(int residule)
	{
		this.residule=residule;
	}
	public void setstatus(int status)
	{
		this.status=status;
	}
	
	public static ArrayList<FileInfo> getfilelist_allbyuserID(String userID) throws Exception
	{
		ArrayList<FileInfo> fileList_ArrayList = new ArrayList<FileInfo>();
		String sqlString = "select * from FileList where userID='"+userID+"';";
		Connection connection = DBHandler.getconnection();
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(sqlString);
		while(rs.next())
		{
			FileInfo temp = new FileInfo(rs.getString("userID"),rs.getString("filename"),rs.getInt("pixelsize_x"),
					rs.getInt("pixelsize_y"), rs.getDouble("env_max_x"), rs.getDouble("env_min_x"), rs.getDouble("env_max_y"),
					rs.getDouble("env_min_y"), rs.getString("CRS_decode"), rs.getInt("split_size"),rs.getInt("split_num_x"), rs.getInt("split_num_y"), 
					rs.getInt("residule"),rs.getInt("status"));
			fileList_ArrayList.add(temp);
		}
		statement.close();
		connection.close();
		return fileList_ArrayList;
	}
	
	public static FileInfo getfileInfobyuserIDandfilename(String userID,String filename) throws Exception
	{
		String sqlString = "select * from FileList where userID='"+userID+"' and filename = '"+filename+"';";
		System.out.println(sqlString);
		Connection connection = DBHandler.getconnection();
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(sqlString);
		FileInfo resultfile = null;
		while(rs.next())
		{
			resultfile = new FileInfo(rs.getString("userID"),rs.getString("filename"),rs.getInt("pixelsize_x"),
					rs.getInt("pixelsize_y"), rs.getDouble("env_max_x"), rs.getDouble("env_min_x"), rs.getDouble("env_max_y"),
					rs.getDouble("env_min_y"), rs.getString("CRS_decode"),rs.getInt("split_size"), rs.getInt("split_num_x"), rs.getInt("split_num_y"), 
					rs.getInt("residule"),rs.getInt("status"));
		}
		statement.close();
		connection.close();
		return resultfile;
	}
	
	
	public boolean updatefilelist()
	{
		String sqlString = "update FileList set "
				+ "pixelsize_x="+this.pixelsize_x
				+ ",pixelsize_y="+this.pixelsize_y
				+ ",env_max_x="+this.env_max_x
				+ ",env_min_x="+this.env_min_x
				+ ",env_max_y="+this.env_max_y
				+ ",env_min_y="+this.env_min_y
				+ ",CRS_decode="+"'"+this.cRS_decode+"'"
				+ ",split_size="+this.split_size
				+ ",split_num_x="+this.split_num_x
				+ ",split_num_y="+this.split_num_y
				+ ",residule="+this.residule
				+",status="+this.status
				+ " WHERE userID='"+this.userID+"'"
				+ " AND filename="+"'"+this.filename+"'"
				+";";
//		System.out.println(sqlString);
		try{
		Connection con = DBHandler.getconnection();
		Statement statement = con.createStatement();
		statement.executeUpdate(sqlString);
		statement.close();
		con.close();
		return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
	}
	
	public static boolean delete(String userID,String filename)
	{
		String sqlString = 
				"DELETE FROM FileList WHERE userID="
				+ "'"+userID
				+ "' AND filename="
				+"'"+filename+"';";
		try{
			Connection connection = DBHandler.getconnection();
			Statement statement = connection.createStatement();
			statement.execute(sqlString);
			statement.close();
			connection.close();
			return true;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean insertnewfileinfo(String userID,String filename,int status)
	{
		String sqlString = "insert into FileList(userID,filename,status) values("
				+ "'"+userID
				+ "','"+filename+"',"
				+status+");";
		try{
			Connection connection = DBHandler.getconnection();
			Statement statement = connection.createStatement();
			statement.execute(sqlString);
			statement.close();
			connection.close();
			return true;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 按分块情况获得分块编号列表
	 * @return
	 */
	public ArrayList<String> getSplitCode()
	{
		ArrayList<String> splitedName = new ArrayList<String>();
		for(int iy=0;iy<this.split_num_y;++iy)
		{
			for(int ix=0;ix<this.split_num_x;++ix)
			{
				splitedName.add("_"+ix+"_"+iy);
			}
		}
		return splitedName;
	}
	
	public static boolean isExist(String id,String filename) throws Exception
	{
		String sqlString = "select * from FileList where userID='"+id+"' and filename = '"+filename+"';";
		Connection connection = DBHandler.getconnection();
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(sqlString);
		if(rs.next())return true;
		else return false;
	}

}
